package org.opencv.ImgSDK;

import android.content.Intent;
import android.hardware.display.VirtualDisplay;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import org.greenrobot.eventbus.EventBus;
import org.opencv.ImgSDK.TrackSDK;

public class TrackLibActivity extends AppCompatActivity {
    private static final int SCREEN_SHOT = 1;
    public static Handler handler = new Handler(Looper.getMainLooper());
    private int dpi;
    private MediaProjection.Callback mCallback;
    private int mHeightPixels;
    private VirtualDisplay mVirtualDisplay;
    private int mWidthPixels;

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.SupportActivity, android.support.v4.app.FragmentActivity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        initSize();
        TrackSDK.getInstance().setOnCallDestroyListener(new TrackSDK.OnCallDestroyListener() {
            /* class org.opencv.ImgSDK.TrackLibActivity.AnonymousClass1 */

            @Override // org.opencv.ImgSDK.TrackSDK.OnCallDestroyListener
            public void result(boolean z) {
                TrackLibActivity.this.destroyRecord();
            }
        });
        if (!TrackSDK.hasPermission) {
            requestPermission();
        } else {
            startRecording();
        }
    }

    private void initSize() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
        this.mWidthPixels = displayMetrics.widthPixels;
        this.mHeightPixels = displayMetrics.heightPixels;
        this.dpi = displayMetrics.densityDpi;
    }

    private void requestPermission() {
        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getApplicationContext().getSystemService("media_projection");
        TrackSDK.projectionManager = mediaProjectionManager;
        startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), 1);
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.FragmentActivity
    public void onActivityResult(int i, int i2, Intent intent) {
        if (i == 1 && i2 == -1) {
            TrackSDK.hasPermission = true;
            TrackSDK.resultCode = i2;
            TrackSDK.data = intent;
            startRecording();
            return;
        }
        TrackSDK.hasPermission = false;
        if (TrackSDK.mOnStrartRecordListener != null) {
            TrackSDK.mOnStrartRecordListener.result(false, "");
        }
        destroyRecord();
    }

    private void startRecording() {
        moveTaskToBack(true);
        handler.postDelayed(new Runnable() {
            /* class org.opencv.ImgSDK.TrackLibActivity.AnonymousClass2 */

            public void run() {
                try {
                    if (TrackSDK.projection == null) {
                        TrackSDK.projection = TrackSDK.projectionManager.getMediaProjection(TrackSDK.resultCode, TrackSDK.data);
                        if (TrackSDK.projection != null) {
                            TrackLibActivity.this.mCallback = new MediaProjection.Callback() {
                                /* class org.opencv.ImgSDK.TrackLibActivity.AnonymousClass2.AnonymousClass1 */

                                public void onStop() {
                                    super.onStop();
                                    if (TrackSDK.mOnDestroyRecordListener != null) {
                                        TrackSDK.mOnDestroyRecordListener.result(true, "");
                                    }
                                }
                            };
                            TrackSDK.projection.registerCallback(TrackLibActivity.this.mCallback, null);
                            TrackSDK.imageReader = ImageReader.newInstance(TrackLibActivity.this.mWidthPixels, TrackLibActivity.this.mHeightPixels, 1, 15);
                            TrackLibActivity.this.mVirtualDisplay = TrackSDK.projection.createVirtualDisplay("shot", TrackLibActivity.this.mWidthPixels, TrackLibActivity.this.mHeightPixels, TrackLibActivity.this.dpi, 16, TrackSDK.imageReader.getSurface(), null, null);
                            TrackSDK.imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
                                /* class org.opencv.ImgSDK.TrackLibActivity.AnonymousClass2.AnonymousClass2 */

                                public void onImageAvailable(ImageReader imageReader) {
                                    if (!TrackSDK.hasStart) {
                                        TrackSDK.hasStart = true;
                                        if (TrackSDK.mOnStrartRecordListener != null) {
                                            Log.d("TrackLibActivity", "TrackSDK返回启动成功");
                                            TrackSDK.mOnStrartRecordListener.result(true, "");
                                        }
                                    }
                                }
                            }, null);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    TrackLibActivity.this.finish();
                }
            }
        }, 50);
    }

    private void relealse() {
        try {
            VirtualDisplay virtualDisplay = this.mVirtualDisplay;
            if (virtualDisplay != null) {
                virtualDisplay.release();
                this.mVirtualDisplay = null;
            }
            if (TrackSDK.projection != null) {
                TrackSDK.projection.stop();
                TrackSDK.projection = null;
                TrackSDK.imageReader.close();
                TrackSDK.imageReader = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            TrackSDK.hasPermission = false;
            finish();
        }
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onDestroy() {
        super.onDestroy();
        destroyRecord();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void destroyRecord() {
        try {
            EventBus.getDefault().unregister(this);
            relealse();
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
